(module-name <console.scm.manager>)
(define REQUEST_OTHER (console.manager.TaskManager:.REQUEST_OTHER))
(define REQUEST_RUN (console.manager.TaskManager:.REQUEST_RUN))
(define REQUEST_PAUSE (console.manager.TaskManager:.REQUEST_PAUSE))
(define REQUEST_CANCEL (console.manager.TaskManager:.REQUEST_CANCEL))
(define STATE_OTHER (console.manager.TaskManager:.STATE_OTHER))
(define STATE_RUN (console.manager.TaskManager:.STATE_RUN))
(define STATE_PAUSE (console.manager.TaskManager:.STATE_PAUSE))
(define STATE_DONE (console.manager.TaskManager:.STATE_DONE))
(define STATE_FAIL (console.manager.TaskManager:.STATE_FAIL))
(define STATE_ERROR (console.manager.TaskManager:.STATE_ERROR))

(define (task-request task-id . args)
	(case (length args)
    ((0)
  		(console.manager.TaskManager:getRequest task-id))
		((1)
  		(console.manager.TaskManager:setRequest task-id (car args)))
		(else
			(throw (console.ConsoleException (string-append "task-request requires 0 or 1 arguments, not " (number->string (length args))))))))
(define (task-state-info task-id . args)
	(case (length args)
		((0)
		  (console.manager.TaskManager:getStateInfo task-id))
		((1)
		  (console.manager.TaskManager:setStateInfo task-id (car args)))
		(else
			(throw (<console.ConsoleException> (string-append "task-state-info requires 0 or 1 arguments, not " (number->string (length args))))))))
(define (task-state task-id . args)
  (case (length args)
		((0)
			(console.manager.TaskManager:getState task-id))
		((1)
			(console.manager.TaskManager:setState task-id (car args)))
		((2)
			(console.manager.TaskManager:setState task-id (car args) (cadr args)))
		(else
			(throw (<console.ConsoleException> (string-append "task-state requires 0, 1 or 2 arguments, not " (number->string (length args))))))))
(define (task-progress task-id . args)
	(case (length args)
		((0)
  		(console.manager.TaskManager:setProgress task-id))
		((1)
		  (console.manager.TaskManager:setProgress task-id (car args)))
		((2)
			(console.manager.TaskManager:setProgress task-id (car args) (cadr args)))
		(else
			(throw (<console.ConsoleException> (string-append "task-progress requires 0, 1 or 2 arguments, not " (number->string (length args))))))))
(define (task-log task-id message)
  (console.manager.TaskManager:log task-id message))
(define (task-new name)
  (console.manager.TaskManager:newTask name))
(define (task-cancel . args)
	(for-each
		(lambda (task-id)
		  (task-request task-id REQUEST_CANCEL))
		args))
(define (task-pause . args)
	(for-each
		(lambda (task-id)
		  (task-request task-id REQUEST_PAUSE))
		args))
(define (task-cpu-time task-id . args)
  (if (> (length args) 0)
    (console.manager.TaskManager:incCPUTime task-id (car args))
    (console.manager.TaskManager:getCPUTime task-id)))
(define (task-run . args)
	(for-each
		(lambda (task-id)
  		(task-request task-id REQUEST_RUN))
		args))
(define (task-exception task-id)
	(console.manager.TaskManager:getException task-id))
(define (task-thread task-id)
	(console.manager.TaskManager:getThread task-id))
(define (task-show-all)
  (console.manager.TaskManager:showTaskList))
(define (task-clear task-id)
  (console.manager.TaskManager:clearTask task-id))
(define (task-clear-all)
  (console.manager.TaskManager:clearTaskList))
(define test-count 1)
(define a "a")
(define (test-task-exe name thunk . args)
  (let ((parent-task-id (if (> (length args) 0) (car args) 0))
        (rely-on-parent (if (> (length args) 1) (cadr args) #t)))
    (newline)
    (display test-count)
    (display " ")
    (display name)
    (display " ")
    (display thunk)
    (display " ")
    (display args)
    (set! test-count (+ test-count 1))
    (set! a args)
		test-count))

;(define-class <execlass> (<console.manager.TaskProcess>)
;  ((tt :: <java.lang.Object>))
;  ((*init* t :: <java.lang.Object>) (set! tt t))
;	((start task-id :: <int>) :: <void> (tt task-id)))

(define (task-exe name thunk action . args)
  (let ((parent-task-id (if (> (length args) 0) (car args) 0))
        (rely-on-parent (if (> (length args) 1) (cadr args) #t))
        (sub-task (if (> (length args) 2) (caddr args) #f))
        (log-file-name (if (> (length args) 3) (cadddr args) #!null)))
		(console.manager.TaskManager:exe name (<console.manager.ScmTaskProcess> thunk action) log-file-name parent-task-id rely-on-parent sub-task)))
;(define (exe-sequence sequence-list . arg)
;  (let ((parent (if (> (length arg) 0) (car arg) 0))
;        (sub-task (if (> (length arg) 1) (cadr arg) #f)))
;    (letrec ((next (lambda (l parent)
;                     (if (> (length l) 0)
;                       (if (list? (car (reverse l)))
;                         (next (car (reverse l)) (next (reverse (cdr (reverse l))) parent))
;                         (let ((rely-flag (not (eq? (string-ref (car l) 0) #\~))))
;                           (task-exe
;                             (car l)
;                             (lambda (task-id)
;                               (apply
;                                 (cadr l)
;                                 (append (list task-id) (if (> (length l) 2) (cddr l) (list )))))
;                             parent
;                             rely-flag
;                             sub-task)))
;                       parent))))
;      (next sequence-list parent))))
(define (make-task thunk . args)
  (lambda (task-id)
    (apply thunk (append (list task-id) args))))
(define test-sequence
  (list
    (list "test"
      (lambda (task-id x) (task-state-info task-id "jiffy"))
      2
      (list "test"
        (lambda (task-id x) (task-state-info task-id "jiffy"))
        2))
    (list "test"
      (lambda (task-id x) (task-state-info task-id "jiffy"))
      2)
    (list "test"
      (lambda (task-id x) (task-state-info task-id "jiffy"))
      2)
    (list "test"
      (lambda (task-id x) (task-state-info task-id "jiffy"))
      2)
    (list "test1"
        (lambda (task-id x) (throw (<console.ConsoleException> x)))
        1
        (list "test2"
          (lambda (task-id x) (task-state-info task-id "jiffy"))
          2)
        (list "~test3"
          (lambda (task-id x) (task-state-info task-id "jiffy"))
          2))))

